package yxy.game.pm2.command.union.tech;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionMessage;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;
import yxy.game.pm2.module.union.player.UnionPlayer;
import yxy.game.pm2.module.union.player.tech.UnionPlayerTech;
import yxy.game.pm2.module.union.player.tech.UnionTechHandle;

/** 联盟科技 联盟科技 */
@MessageType(proto = Command.UnionTech, classtype = UnionMessage.UnionTechRequest.class)
public class CMD_UnionTech extends Command<UnionMessage.UnionTechRequest> {

	@Override
	public void execute(MessageSource messageSource, UnionMessage.UnionTechRequest request) {
		String playerCode = messageSource.getTag();

		UnionMessage.UnionTechResponse.Builder response = UnionMessage.UnionTechResponse.newBuilder();

		UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
		Result result = new UnionHandle().checkPlayerUnion(entry);
		if (!result.equals(Result.UnionIn)) {
			send(messageSource, response, result);
			return;
		}
		UnionPlayer unionPlayer = entry.getUnionPlayer();
		UnionPlayerTech playerTech = unionPlayer.getTech();

		UnionTechHandle techHandle = new UnionTechHandle();
		for (yxy.game.pm2.module.union.player.tech.UnionTech tech : playerTech.getTechList()) {
			response.addTech(techHandle.packTech(tech));
		}

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, UnionMessage.UnionTechResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}